package com.cdtu.store.auth;

import com.cdtu.store.auth.entity.UserInfo;
import com.cdtu.store.auth.utils.JwtUtils;
import com.cdtu.store.auth.utils.RsaUtils;

import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @author dongyuhao
 */
public class Main {
    private static final String pubKeyPath = "E:\\tmp\\rsa.pub";

    private static final String priKeyPath = "E:\\tmp\\rsa.pri";

    private static PublicKey publicKey;

    private static PrivateKey privateKey;

    public static void main(String[] args) throws Exception {
        //testRsa();
        testGetRsa();
        testGenerateToken();
        testParseToken();
    }

    public static void testRsa() throws Exception {
        RsaUtils.generateKey(pubKeyPath, priKeyPath, "234");
    }
    public static void testGetRsa() throws Exception {
        publicKey = RsaUtils.getPublicKey(pubKeyPath);
        privateKey = RsaUtils.getPrivateKey(priKeyPath);
    }

    public static void testGenerateToken() throws Exception {
        // 生成token
        String token = JwtUtils.generateToken(new UserInfo(20L, "jack"), privateKey, 5);
        System.out.println("token = " + token);
    }

    public static void testParseToken() throws Exception {
        String token = "eyJhbGciOiJSUzI1NiJ9.eyJpZCI6MjAsInVzZXJuYW1lIjoiamFjayIsImV4cCI6MTU5NDAxNzk4M30.MXXgYqoEthFyd4sijWcbtOUDjHmXFMUAPjWlTlwrhOJqO_2rAQZZ9WvC6tTdMf41U-MdG_pHOUu5wWCTXwJ9ssTnHO3bNcmAdCLT1NJk-vkv-0Y7IhF8fYT4llL14wdE1qKzznhrGw35g24c9ROYtAs80eStwqHK8FJHr257UK4";

        // 解析token
        UserInfo user = JwtUtils.getInfoFromToken(token, publicKey);
        System.out.println("id: " + user.getId());
        System.out.println("userName: " + user.getUsername());
    }
}
